package cn.tedu.erp.project.service.impl;

import cn.tedu.erp.project.ex.ServiceException;
import cn.tedu.erp.project.mapper.PermissionMapper;
import cn.tedu.erp.project.mapper.RolePermissionMapper;
import cn.tedu.erp.project.pojo.dto.PermissionAddNewDTO;
import cn.tedu.erp.project.pojo.dto.PermissionUpdateDTO;
import cn.tedu.erp.project.pojo.entity.Permission;
import cn.tedu.erp.project.pojo.vo.PermissionListVO;
import cn.tedu.erp.project.pojo.vo.PermissionStandardVO;
import cn.tedu.erp.project.service.IPermissionService;
import cn.tedu.erp.project.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.List;

@Slf4j
@Service
@Transactional
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    /**
     * 添加权限
     *
     * @param permissionAddNewDTO
     */
    @Override
    public void addNew(PermissionAddNewDTO permissionAddNewDTO) {
        log.debug("开始处理添加权限的业务,参数为:{}", permissionAddNewDTO);
        //从参数对象中获取权限的名称
        String name = permissionAddNewDTO.getName();
        //调用permissionMapper的countByName()执行统计
        int countName = permissionMapper.countByName(name);
        //判断统计结果是否大于0
        if (countName > 0) {
            //是:抛出异常
            String message = "添加失败,添加的权限名称已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //获取参数对象中的value值
        String value = permissionAddNewDTO.getValue();
        countName = permissionMapper.countByValue(value);
        //判断是否value已经存在
        if (countName > 0) {
            //是:抛出异常
            String message = "添加失败,添加的权限名称已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        //创建对象
        Permission permission = new Permission();
        //复制属性
        BeanUtils.copyProperties(permissionAddNewDTO, permission);
        // 调用permissionMapper的insert方法
        int row = permissionMapper.insert(permission);
        if (row != 1) {
            String message = "添加权限失败,服务器忙,稍后再试";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    /**
     * 根据id删除权限
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除权限的业务,参数为id={}", id);
        //管理员,部门,角色,权限的权限不能被删除
        if (id < PRIVILEGE) {
            String message = "删除失败,被删除的权限不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        //查询被删除的对象是否存在,否:报异常
        PermissionStandardVO permissionStandardVO = permissionMapper.getStandardById(id);
        if (permissionStandardVO == null) {
            String message = "删除失败,被删除的权限不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        //调用permissionMapper的delete方法
        int row = permissionMapper.deleteById(id);
        if (row != 1) {
            String message = "删除失败,服务器忙,稍后再试!!!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
        //调用rolePermissionMapper的delete方法
        row = rolePermissionMapper.deleteById(id);
        if (row < 1) {
            String message = "删除失败,服务器忙,稍后再试???";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_DELETE, message);
        }
    }

    /**
     * 根据id修改权限
     *
     * @param id
     */
    @Override
    public void updateById(Long id, PermissionUpdateDTO permissionUpdateDTO) {
        log.debug("开始处理修改权限的业务,参数为id={}", id);
        if (id < PRIVILEGE) {
            String message = "修改失败,被修改的权限不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //查询要修改的权限是否存在
        PermissionStandardVO permissionStandardVO = permissionMapper.getStandardById(id);
        if (permissionStandardVO == null) {
            String message = "修改失败,权限不存在";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        log.debug("根据id={}获取的权限名称是否存在,结果为{}", id, permissionStandardVO.getName());
        //查询要修改的权限名称是否同名
        int count = permissionMapper.countByNameAndNotId(id, permissionUpdateDTO.getName());
        if (count > 0) {
            String message = "修改失败,权限名称已经被占用";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        Permission permission = new Permission();
        //复制属性
        BeanUtils.copyProperties(permissionUpdateDTO, permission);
        //设置id
        permission.setId(id);
        int rows = permissionMapper.update(permission);
        if (rows != 1) {
            String message = "修改权限失败,服务器忙,请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    /**
     * 根据id查询权限详情
     *
     * @param id
     * @return
     */
    @Override
    public PermissionStandardVO getStandardById(Long id) {
        log.debug("开始处理查询权限详情的业务,参数为id={}", id);
        if (id < PRIVILEGE) {
            String message = "查询失败,被查询的权限不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        //调用permissionMapper的getStandardVO方法
        PermissionStandardVO permissionStandardVO = permissionMapper.getStandardById(id);
        if (permissionStandardVO == null) {
            String message = "查询失败,被查询的权限不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return permissionStandardVO;
    }

    /**
     * 查询权限列表
     *
     * @return
     */
    @Override
    public List<PermissionListVO> list() {
        log.debug("开始处理[查询权限列表]的业务,无参数");
        //调用permissionMapper的list方法
        List<PermissionListVO> list = permissionMapper.list();
        //使用list的迭代器
        Iterator<PermissionListVO> iterator = list.iterator();
        //查询小于基本权限id的对象,直到将该对象从list集合中删除
        while (iterator.hasNext()) {
            PermissionListVO item = iterator.next();
            if (item.getId() < PRIVILEGE) {
                iterator.remove();
            }
        }
        return list;
    }
}
